'/
' MOTEP - Massive Online Text Environment Project
' --------------------------------------------------------------------------------
' Shared Code
' 
' Output.bmx
' 
' (c) Anton Berlin. All Rights Reserved.
'/

include "..\Shared\Colors_h.bmx"

const MAX_OUTPUT_LINES:int = 33
const EMPTY:string = "!!EMPTY!!"

global output:string[34] '// Array containing a list of the last 34 lines of the output.



'// Draw all the output.
function drawOutput()

	local count:int = 0, x:int = 15, y:int = 100
	local foundBracket:byte = false
	local color:string
	
	SetImageFont(defaultFont)
	clrDefault()
	
	for local i:string = eachin output
	
		x = 15
		
		clrDefault()
		
		if (output[count] <> EMPTY) then
		
			for local j:int = 1 to Len(i)
			
				if (Mid(i, j, 1) = "{") ..
					foundBracket = true
				
				if (foundBracket = false) then
				
					'// Make sure the text never gets longer than the screen-size.
					if (x > RES_X - 8) ..
						x = 15
					
					'// Draw the next character.
					DrawText(Mid(i, j, 1), x, y)
					
					'// Move the x marker one column to the right.
					x = x + 8
				
				else
				
					color = color + Mid(i, j, 1)
				
				end if
				
				if (Mid(i,j,1) = "}") then
				
					foundBracket = false
					
					select (Lower(color))
					
						'// Regular colors.
						case "{red}"			clrRed()
						case "{blue}"			clrBlue()
						case "{green}"			clrGreen()
						case "{yellow}"		clrYellow()
						case "{turcose}"		clrTurcose()
						case "{magenta}"		clrMagenta()
						case "{purple}"		clrPurple()
						case "{orange}"		clrorange()
						case "{brown}"			clrBrown()
						case "{wood}"			clrWood()
						case "{pink}"			clrPink()
						case "{gray}"			clrGray()
						case "{grey}"			clrGray()
					
						'// Bright colors.
						case "{brightred}"		clrBrightRed()
						case "{brightblue}"		clrBrightBlue()
						case "{brightgreen}"	clrBrightGreen()
						case "{brightyellow}"	clrBrightYellow()
						case "{brightturcose}"	clrBrightTurcose()
						case "{brightmagenta}"	clrBrightMagenta()
						case "{brightpurple}"	clrBrightPurple()
						case "{brightorange}"	clrBrightorange()
						case "{brightbrown}"	clrBrightBrown()
						case "{brightwood}"		clrBrightWood()
						case "{brightpink}"		clrBrightPink()
						case "{brightgray}"		clrBrightGray()
						case "{brightgrey}"		clrBrightGray()
					
						'// Dark colors.
						case "{darkred}"		clrDarkRed()
						case "{darkblue}"		clrDarkBlue()
						case "{darkgreen}"		clrDarkGreen()
						case "{darkyellow}"		clrDarkYellow()
						case "{darkturcose}"	clrDarkTurcose()
						case "{darkmagenta}"	clrDarkMagenta()
						case "{darkpurple}"		clrDarkPurple()
						case "{darkorange}"		clrDarkorange()
						case "{darkbrown}"		clrDarkBrown()
						case "{darkwood}"		clrDarkWood()
						case "{darkpink}"		clrDarkPink()
						case "{darkgray}"		clrDarkGray()
						case "{darkgrey}"		clrDarkGray()
					
						'// Special colors.
						case "{black}"			clrBlack()
						case "{white}"			clrWhite()
						case "{common}"		clrCommon()
						case "{gothicgray}"		clrCustom(175,185,210)
						case "{mana}"			clrCustom(0,150,250)
						case "{oxygen}"		clrCustom(50,75,255)
						case "{quest}"			clrQuest()
						case "{poison}"		clrPoison()
					
						'// Default color.
						case "{default}"		clrDefault()
					
					end select
					
					color = null
				
				end if
			
			next
		
		end if
		
		y = y + 13
		count = count + 1
	
	next
	
	clrWhite()

end function

'// Reset the output array, so that it displays no text (kind of like how
'// Cls() works in Blitz).
function clearOutput()

	local count:int
	
	for local i:string = eachin output
	
		output[count] = EMPTY
		count = count + 1
	
	next
	
	WriteLine(logHandle, "Cleared the global output array.")

end function

'// Write text to the output array.
function write:int(str:string = null)

	local written:int = false
	
	if (str = null) ..
		str = " "
	
	for local i:int = 0 to MAX_OUTPUT_LINES
	
		if (output[i] = EMPTY or output[i] = null) then
		
			output[i] = str
			written = true
			exit
		
		end if
	
	next
	
	if written = false then
	
		'// Delete the item in the bottom and move everything down one row.
		for local j:int = 0 until MAX_OUTPUT_LINES
		
			output[j] = output[j + 1]
			output[j + 1] = null
		
		next
		
		'// Write the new line to the new empty row in the top.
		output[MAX_OUTPUT_LINES] = str
		written = true
	
	end if
	
	if (written = true) ..
		return true ..
	else ..
		return false

end function

'// Write text to the output array without going to a new line. Use the
'// spaces parameter to make the text start in a certain column, no matter
'// what text is behind it.
function writeSame:int(str:string = null, spaces:int = 0)

	local written:int = false
	
	if (str = null or Len(str) > 300) ..
		return false
		
	for local i:int = 0 until MAX_OUTPUT_LINES
	
		if (output[i + 1] = EMPTY or output[i + 1] = null) then
		
			if (spaces > 0) then
			
				local lenOfLine:int = 0
				local ammountOfSpaces:int = 0
				local foundBracket:int = false
				
				for local j:int = 1 To Len(output[i])
				
					if (Mid(output[i], j, 1) = "{") ..
						foundBracket = true
					if (not foundBracket) ..
						lenOfLine = lenOfLine + 1
					if (Mid(output[i], j, 1) = "}") ..
						foundBracket = false
				
				next
				
				if (not Len(output[i])) lenOfLine = 0
				
				ammountOfSpaces = spaces - lenOfLine
				
				if (ammountOfSpaces > 0) then
				
					for local space:int = 0 To ammountOfSpaces
					
						if (output[i] = EMPTY or output[i] = null) ..
							output[i] = " " ..
						else ..
							output[i] = output[i] + " "
						
					next
				
				end if
			
			end if
			
			if (output[i] = EMPTY or output[i] = null) ..
				output[i] = str ..
			else ..
				output[i] = output[i] + str
			
			written = true
			exit
			
		end if
	
	next
	
	if (written = false) then
	
		if (spaces > 0) then
		
			local lenOfLine:int = 0
			local ammountOfSpaces:int = 0
			local foundBracket:int = false
			
			for local j:int = 1 to Len(output[MAX_OUTPUT_LINES])
			
				if (Mid(output[MAX_OUTPUT_LINES], j, 1) = "{") ..
					foundBracket = true
				if (not foundBracket) ..
					lenOfLine = lenOfLine + 1
				if (Mid(output[MAX_OUTPUT_LINES], j, 1) = "}") ..
					foundBracket = false
			
			next
			
			if (not Len(output[MAX_OUTPUT_LINES])) ..
				lenOfLine = 0
			
			ammountOfSpaces = spaces - lenOfLine
			
			if (ammountOfSpaces > 0) then
			
				for local space:int = 0 to ammountOfSpaces
				
					if (output[MAX_OUTPUT_LINES] = NULL_DATA or output[MAX_OUTPUT_LINES] = null) ..
						output[MAX_OUTPUT_LINES] = " " ..
					else ..
						output[MAX_OUTPUT_LINES] = output[MAX_OUTPUT_LINES] + " "
				
				next
			
			end if
		
		end if
		
		if (output[MAX_OUTPUT_LINES] = EMPTY or output[MAX_OUTPUT_LINES] = null) ..
			output[MAX_OUTPUT_LINES] = str ..
		else ..
			output[MAX_OUTPUT_LINES] = output[MAX_OUTPUT_LINES] + str
		
		written = true
	
	end if
	
	if (written = true) ..
		return true ..
	else ..
		return false

end function

'// Capitalize a string.
function cap:string(str:string = null)

	if (Len(str)) ..
		return Upper(Mid(str, 1, 1)) + Mid(str, 2, Len(str) - 1)

end function

